#!/bin/bash
#
# pkcsslotd        Starts pkcsslotd
#
# Authors:	Kent E. Yoder <yoder1@us.ibm.com>
#		Serge E. Hallyn <serue@us.ibm.com>
#               Daniel H. Jones <danjones@us.ibm.com>
#
# chkconfig: - 50 50
# description: pkcsslotd is a daemon which manages cryptographic hardware \
# tokens for the openCryptoki package.

. /etc/init.d/functions

PIDFILE=/var/run/pkcsslotd.pid
LOCKFILE=/var/lock/subsys/pkcsslotd
SLOTDBIN=@METHOD_PATH@/pkcsslotd
CONFSTART=@METHOD_PATH@/pkcs11_startup

[ -f $SLOTDBIN ] || exit 5
[ -f $CONFSTART ] || exit 5

start() {
 	echo -n $"Starting pkcsslotd: "

        # Generate the configuration information
        $CONFSTART

	## Start daemon with startproc(8). If this fails
	## the echo return value is set appropriate.
        if [ ! -f $PIDFILE ]; then
           # pid file does not exist
           daemon --force $SLOTDBIN
        elif ! ps -h --pid `cat $PIDFILE` | grep "$SLOTDBIN" 2>&1 >/dev/null; then
           # pid file exists but named pid not
           rm -f $PIDFILE
           daemon --force $SLOTDBIN
        else
           # just to have "failed" message
           daemon $SLOTDBIN
        fi

	echo
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch $LOCKFILE
	return $RETVAL
}	

stop() {
	echo -n $"Shutting down pkcsslotd:"
	killproc pkcsslotd -TERM
	echo
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	return $RETVAL
}

restart() {
	stop
	start
}

RETVAL=0
umask 077

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
  	status pkcsslotd $SLOTDBIN
	;;
  restart|reload)
  	restart
	;;
  condrestart)
  	[ -f $LOCKFILE ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
esac

exit $?
